home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Tools&Utilities / Plotfoil 3.2 / ps_procs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-09  |  8.7 KB  |  311 lines  |  [TEXT/MMCC]

  1.  
  2. /*
  3.  * This file is part of plotfoil v3
  4.  * Copyright 1992,1994,1995 Shamim Mohamed
  5.  *
  6.  * ps_procs.c : PS procedures for plotfoil.
  7.  *
  8.  * warning: not for the faint of heart.
  9.  */
  10.  
  11. #include "plotfoil.h"
  12. #include "externs.h"
  13.  
  14. static void draw_spar(float offset, float thick, point_t *points, int n);
  15. static void put_title(char *);
  16.  
  17. static char info[BUFSIZ];
  18.  
  19. static char *ps_header = "\
  20. %%!PS-Adobe-2.0 EPSF-1.2\n\
  21. %%%%BoundingBox: %d %d %d %d\n";
  22.  
  23. static char *multipage_header = "\
  24. %%!PS-Adobe-2.0\n\
  25. %%%%Pages: %d\n";
  26.  
  27. static char *ps_procs = "\
  28. %%%%Orientation: Landscape\n\
  29. %%%%Title: %s\n\
  30. %%%%DocumentFonts: Times-Roman\n\
  31. %%%%EndComments\n\
  32. /S {stroke} bind def /L {lineto} bind def /M {moveto} bind def\n\
  33. /rshow {dup stringwidth pop neg 0 rmoveto show} bind def\n\
  34. /X {moveto XH2 0 rmoveto XH neg 0 rlineto XH2 0 rmoveto\n\
  35.     0 XH2 rmoveto 0 XH neg rlineto 0 XH2 rmoveto} bind def\n\
  36. /sf {setfont} bind def /drawbbox {4 dict begin\n\
  37.    /y2 exch def /x2 exch def /y1 exch def /x1 exch def\n\
  38.    x1 y1 X x1 y2 X x2 y2 X x2 y1 X\n\
  39. end} bind def\n\
  40. /drawrect {4 dict begin newpath\n\
  41.    /y2 exch def /x2 exch def /y1 exch def /x1 exch def\n\
  42.    x1 y1 moveto x1 y2 lineto x2 y2 lineto x2 y1 lineto closepath\n\
  43.    stroke\nend} bind def\n\
  44. /cr {currentpoint exch pop sub neg moveto} bind def\n\
  45. /LS {\n\
  46.    %d 0 translate 90 rotate newpath} bind def\n\
  47. /C {curveto} bind def\n\
  48. /XH %d def /XH2 %d def\n";
  49.  
  50. /*
  51.  * This sets up the multipage plots. The sheets are all of height pht and
  52.  * width pwt (in portrait mode). To allow for the fudge factor we do a
  53.  * translate first.
  54.  */
  55. static char *multipage_procs = "\
  56. /plarge {\n\
  57.   /label exch def /i exch def /j exch def /bproc exch def\n\
  58.   fy neg fx translate\n\
  59.   F sf m dup pht add moveto label show\n\
  60.   m m moveto 0 pht rlineto pwt 0 rlineto 0 pht neg rlineto closepath\n\
  61.   gsave 0 setlinewidth stroke grestore clip\n\
  62.   pwt j mul neg pht i mul neg translate bproc showpage\n\
  63. } def\n\
  64. /top m pht add def\n\
  65. /right m pwt add def\n";
  66.  
  67. void init_output(char *s)
  68. {
  69.    float tx, ty, vsizeup, vsizedn, titles;
  70.  
  71.    titles = TITLE_SIZE + VS * (1 + nspars + (iflag != 0) + (tflag != 0));
  72.  
  73.    if(tflag)
  74.       vsizeup = vsizedn = templ_h.pts;
  75.    else {
  76.       vsizeup = c_height + CROSS_SIZE/2;
  77.       vsizedn = -c_depth + CROSS_SIZE/2;
  78.    }
  79.  
  80.    /*
  81.     * Here, tx is just a hack, an arbitrary value. ty is chosen so that the
  82.     * plot is centered on the virtual canvas: if the canvas height is H,
  83.     * then the value of ty is:
  84.     *    (H - v_up - v_dn - titles)/2 + v_dn + titles
  85.     */
  86.    tx = templ_le.pts + TITLE_SIZE;
  87.    ty = (nsheets_v*PAGEHEIGHT - vsizeup + vsizedn + titles)/2;
  88.    
  89.    if(multipage)
  90.       fprintf(fout, multipage_header, nsheets_v * nsheets_h);
  91.    else
  92.       fprintf(fout, ps_header,
  93.           PS_WIDTH - (int)(ty + vsizeup + yfudge.pts),
  94.           (int)(tx - CROSS_SIZE/2 + xfudge.pts),
  95.           PS_WIDTH - (int)(ty - vsizedn - titles -CROSS_SIZE/2+yfudge.pts),
  96.           (int)(tx + xfudge.pts + chord.pts + CROSS_SIZE/2));
  97.       
  98.    fprintf(fout, ps_procs, s, PS_WIDTH, CROSS_SIZE, CROSS_SIZE/2);
  99.  
  100.    if(multipage) {
  101.       fprintf(fout,
  102.           "/m %d def /pht %d def /pwt %d def /fx %.1f def /fy %.1f def\n",
  103.           MARGIN, PAGEWIDTH, PAGEHEIGHT, xfudge.pts, yfudge.pts);
  104.       fputs(multipage_procs, fout);
  105.    }
  106.    else
  107.       fprintf(fout, "%.1f %.1f translate\n", xfudge.pts, yfudge.pts);
  108.    
  109.    put_title(s);
  110.  
  111.    fprintf(fout, "/BoP { LS %.4g %.4g translate 0.1 setlinewidth} bind def\n",
  112.        tx, ty);
  113. }
  114.  
  115. void put_curve(x1, y1, x2, y2, x3, y3)
  116. double x1, y1, x2, y2, x3, y3;
  117. {
  118.    x1 *= chord.pts; y1 *= chord.pts; x2 *= chord.pts; y2 *= chord.pts;
  119.    x3 *= chord.pts; y3 *= chord.pts;
  120.    fprintf(fout, "%.2f %.2f %.2f %.2f %.2f %.2f C\n", x1, y1, x2, y2, x3, y3);
  121. }
  122.  
  123. void init_lines(x, y)
  124. double x, y;
  125. {
  126.    x *= chord.pts;
  127.    y *= chord.pts;
  128.  
  129.    fprintf(fout, "/P {\nnewpath\nf sf 20 3 M (%s) show\n", info);
  130.    fprintf(fout, "%.2f %.2f M\n", x, y);
  131.  
  132. }
  133.  
  134. void put_line(x, y)
  135. double x, y;
  136. {
  137.    fprintf(fout, "%.2f %.2f L\n", x*chord.pts, y*chord.pts);
  138. }
  139.  
  140. void draw_double_line(float x1, float y1, float x2, float y2, float spacing)
  141. {
  142.    fprintf(fout, "gsave newpath %.2f %.2f M %.2f %.2f L\n",
  143.        x1*chord.pts, y1*chord.pts, x2*chord.pts, y2*chord.pts);
  144.    if(spacing >= 1.0) {
  145.       fprintf(fout,
  146.           "gsave %.1f setlinewidth 0 setgray stroke\n", spacing);
  147.       fprintf(fout,
  148.           "grestore %.1f setlinewidth 1 setgray stroke grestore\n",
  149.           spacing-0.5);
  150.    }
  151.    else
  152.       fputs("stroke\n", fout);
  153. }
  154.  
  155. void draw_template(float h, float le, float te)
  156. {
  157.    fputs("/Tp {\n", fout);
  158.    fprintf(fout, "-%.2f -%.2f %.2f %.2f drawrect\n", le, h, te+chord.pts, h);
  159.    fprintf(fout, "%.2f -%.2f M (%s) rshow\n", te+chord.pts-2, h-4.0, info);
  160.    fprintf(fout, "%.2f %.2f M (%s) rshow\n", te+chord.pts-2, h-8.0, info);
  161.    fputs("} bind def\n", fout);
  162. }
  163.  
  164. static void put_title(char *s)
  165. {
  166.    char *p;
  167.    int i, nlines=0;
  168.    float offset;
  169.    
  170.    p = s;
  171.    while(*p != '\n' && *p != '\0') p++;
  172.    *p = '\0';
  173.  
  174.    fprintf(fout, "/vs %.4f def\n/SC{show lm vs cr} def\n", VS);
  175.    fprintf(fout, "/Times-Roman findfont dup dup %d scalefont /B exch def\n",
  176.        TITLE_SIZE);
  177.    fprintf(fout, "%d scalefont /F exch def 8 scalefont /f exch def\n", PS);
  178.  
  179.    if(aflag) {
  180.       offset = chord.pts/3.0;
  181.       fputs(" /savedmatrix matrix def /t {savedmatrix currentmatrix\n", fout);
  182.       fprintf(fout, "%.3g 0.0 translate ", offset);
  183.       fprintf(fout, "%.2g rotate ", angle);
  184.       fprintf(fout, "%.3g 0.0 translate} bind def ", -offset);
  185.       fputs("/et { savedmatrix setmatrix } bind def\n", fout);
  186.    }
  187.    else
  188.       fputs("/et{}def/t{}def\n", fout);
  189.  
  190.    fputs("/T {\n   currentpoint pop /lm exch def\n", fout);
  191.    fprintf(fout, "   0 %d rmoveto\n   B sf (%s) SC\n", -TITLE_SIZE, s);
  192.    fprintf(fout, "   F sf\n   (Chord: %.3g%s) SC\n",
  193.        chord.raw_value, chord.units);
  194.    nlines++;
  195.    if(iflag) {
  196.       fprintf(fout,
  197.          "   (with %.3g%s sheeting allowance) SC\n",
  198.          sheeting.raw_value, sheeting.units);
  199.       nlines++;
  200.       sprintf(info, "%s / %.3g%s / %.3g%s", s, chord.raw_value, chord.units,
  201.           sheeting.raw_value, sheeting.units);
  202.    }
  203.    else
  204.       sprintf(info, "%s / %.3g%s", s, chord.raw_value, chord.units);
  205.  
  206.    if(tflag) {
  207.       fprintf(fout, "   (template: height %.3g%s",
  208.          templ_h.raw_value, templ_h.units);
  209.       fprintf(fout, ", %.3g%s at LE,", templ_le.raw_value, templ_le.units);
  210.       fprintf(fout, " %.3g%s at TE) SC\n", templ_te.raw_value,
  211.           templ_te.units);
  212.       nlines++;
  213.    }
  214.  
  215.    if(aflag) {
  216.       fprintf(fout, "   (Twist: %.2g\\312)SC\n", angle);
  217.       nlines++;
  218.    }
  219.  
  220.    for(i = 0; i < nspars; i++, nlines++)
  221.       fprintf(fout,
  222.           "   (Spar at %.3g %% chord, thickness %.3g%s)SC\n",
  223.           spars[i].offset*100, spars[i].thick.raw_value,
  224.           spars[i].thick.units);
  225.    
  226.    fputs("} bind def\n", fout);
  227.  
  228. }
  229.  
  230. void end_output(point_t *points, int npoints)
  231. {
  232.    int i, j;
  233.  
  234.    fputs("} bind def\n\n", fout);
  235.  
  236.    if(multipage)
  237.       fputs("/fproc {\n", fout);
  238.    else
  239.       fputs("%%EndProlog\n\n", fout);
  240.    
  241.    fprintf(fout, "BoP 0 %d neg M T t P et pathbbox newpath drawbbox stroke\n",
  242.        -(int)c_depth);
  243.    if(tflag)
  244.       fputs("Tp\n", fout);
  245.    fprintf(fout,
  246.        "gsave[3 3]0 setdash %.4g 0 moveto %.4g 0 lineto stroke grestore\n",
  247.        -templ_le.pts, chord.pts+templ_te.pts);
  248.  
  249.    fputs("gsave t P clip\n", fout);
  250.    for(i = 0; i < nspars; i++)
  251.       draw_spar(spars[i].offset, spars[i].thick.pts, points, npoints);
  252.    if(aflag)
  253.       fprintf(fout,
  254.          "[3 3]0 setdash 0 setlinewidth 0 0 moveto %.2g 0 lineto stroke\n",
  255.           chord.pts);
  256.    fputs("grestore\n", fout);
  257.  
  258.    if(iflag) {
  259.       fprintf(fout, "gsave t P clip 0 setgray %.4g setlinewidth P stroke\n",
  260.           sheeting.pts*2);
  261.       fprintf(fout, "%.4g setlinewidth 1 setgray P stroke\n",
  262.           sheeting.pts*2-0.5);
  263.       fputs("grestore gsave [2 4] 0 setdash t P stroke grestore\n", fout);
  264.    }
  265.    else
  266.       fputs("t P et stroke\n", fout);
  267.    
  268.    if(multipage) {
  269.       int n=1;
  270.       fputs("} bind def\n%%EndProlog\n\n", fout);
  271.       for(i = 0; i < nsheets_v; i++)
  272.      for(j = 0; j < nsheets_h; j++,n++)
  273.         fprintf(fout, "%%%%Page: %d %d\n{fproc} %d %d((%d,%d)) plarge\n\n",
  274.             n, n, i, j, i+1, j+1);
  275.       fputs("%%EOF\n\n", fout);
  276.    }
  277.    else
  278.       fputs("showpage\n%%EOF\n", fout);
  279.    fflush(fout);
  280. }
  281.  
  282.  
  283. static void draw_spar(float offset, float thick, point_t *points, int n)
  284. {
  285.    float upper=0.0, lower=0.0;
  286.    int i;
  287.  
  288.    i = 0;
  289.    while(i < n) {
  290.       if(offset >= points[i].x) {
  291.      upper = points[i].y +
  292.         (points[i].y - points[i-1].y)/(points[i].x - points[i-1].x)*
  293.            (points[i].x - offset);
  294.      break;
  295.       }
  296.       i++;
  297.    }
  298.    while(i < n) {
  299.       if(offset <= points[i].x) {
  300.      lower = points[i].y +
  301.         (points[i].y - points[i-1].y)/(points[i].x - points[i-1].x)*
  302.            (points[i].x - offset);
  303.      break;
  304.       }
  305.       i++;
  306.    }
  307.  
  308.    draw_double_line(offset, 1.5*upper, offset, 1.5*lower, thick);
  309.    
  310. }
  311.